package cloud.maque.sso.client.service.impl;

import cloud.maque.sso.client.model.ThridSite;
import cloud.maque.sso.client.model.UserModel;
import cloud.maque.sso.client.service.SsoClientAuthUserService;
import cloud.maque.sso.client.service.SsoUserService;
import cloud.maque.sso.client.utils.SoMap;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SsoUserServiceImpl implements SsoUserService {

    @Autowired
    SsoClientAuthUserService ssoClientAuthUserService;

    @Override
    public SaResult process(String body) {

        try {
            SoMap so = SoMap.getSoMap().setJsonString(body);
            System.out.println("返回结果: " + new ObjectMapper().writeValueAsString(so));

            // code不等于200  代表请求失败
            if (so.getInt("code") != 200) {
                return SaResult.error(so.getString("msg"));
            }

            // 根据openid获取其对应的userId
            String userId = ssoClientAuthUserService.getUserIdByOpenid(so.getString("openid"));
            so.set("uid", userId);

            // 根据openid获取其对应的userId
            UserModel user = ssoClientAuthUserService.getUserByUserId(userId);
            if (StrUtil.isEmptyIfStr(user.getOpenId())) {
                ssoClientAuthUserService.updateOpenId(user.getUserId(), so.getString("openid"));
            }

            LoginAndSetValue(userId, user);
            return SaResult.data(so);
        } catch (Exception ex) {
            ex.printStackTrace();
            return SaResult.error(ex.getMessage());
        }
    }

    @Override
    public SaResult loginAndBind(String body, String username, ThridSite thrid) {
        try {
            SoMap so = SoMap.getSoMap().setJsonString(body);
            System.out.println("返回结果: " + new ObjectMapper().writeValueAsString(so));

            // code不等于200  代表请求失败
            if (so.getInt("code") != 200) {
                return SaResult.error(so.getString("msg"));
            }

            // 根据openid获取其对应的userId
            UserModel user = ssoClientAuthUserService.getUserByUserName(username);
            if (StrUtil.isEmptyIfStr(user.getOpenId())) {
                ssoClientAuthUserService.updateOpenId(user.getUserId(), so.getString("openid"));
            }

            //绑定第三方网站
            ssoClientAuthUserService.BindThridSite(user.getUserId(), thrid);
            so.set("uid", user.getUserId());
            LoginAndSetValue(user.getUserId(), user);
            return SaResult.data(so);
        } catch (Exception ex) {
            ex.printStackTrace();
            return SaResult.error(ex.getMessage());
        }
    }

    private void LoginAndSetValue(String userId, UserModel user) {
        // 返回相关参数
        StpUtil.login(userId, new SaLoginModel()
                .setExtra("userName", user.getUserName()));
    }

}
